package cn.tang.tframe.springredis.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.tang.tframe.common.base.MainConfigLoader;
import cn.tang.tframe.springredis.ISpringRedisCache;
import cn.tang.tframe.springredis.QtProjectRedisClient;

public class RedisMqProducerBuilder {

	private static Logger logger = LoggerFactory.getLogger(RedisMqProducerBuilder.class);
	private static class MqBuilderHolder{
		private static RedisMqProducerBuilder instance = new RedisMqProducerBuilder();
	}
	private RedisMqProducerBuilder(){}
	public static RedisMqProducerBuilder getInstance(){
		return MqBuilderHolder.instance;
	}

	public static <T> void createRedisTask(T task, String key) {
		String taskKey = getRedisKey(key);
		try {
			final ISpringRedisCache<Object> springRedisCache =
				QtProjectRedisClient.getSpringRedisCache();
			logger.debug("create Job, task:{},key:{}", task,taskKey);
			springRedisCache.getRedisTemplate().boundListOps(taskKey).leftPush(task);
		} catch (Exception e) {
			logger.error("create Job exception, task:{},taskKey{}", task,taskKey,e);
		}
	}
	private static String getRedisKey(String taskKey) {

		return new StringBuilder().append(
				MainConfigLoader.getInstance().getClientCode()).append(
				":").append(taskKey).toString();
	}
}
